/**
 * Created by djh on 2016/11/8.
 * 银行签约js
 *
 */
define(function(require,exports){
   var common = require("common");
    var bankSelector = require("bankSelector");
    var validate = require("validate");

    //银行支付通道列表
    var payWay = function($obj) {
      common.ajaxHandle({
          url:"/member/member/loadPayWay",
          callback:function(item){
              var html = '<ul class="clearfix">';
              var data = item.data;
              var selectedId="";
              $(data).each(function(i,e){
                  if(i==0) {
                      html += '<li class="fl active" style="margin-right: 10px;color: #666;"><img  data-id="'+e.id+'" src="'+e.icon+'" alt="'+e.name+'"></li>';
                      selectedId = e.id;
                  }else {
                      html += '<li class="fl" style="margin-right: 10px;color: #666;"><img  data-id="'+e.id+'" src="'+e.icon+'" alt="'+e.name+'"></li>';
                  }
              });
              html +='</ul><input type="hidden" name="payway" value="'+selectedId+'"/><p class="c-info"><span class="c-danger">温馨提示：</span>广发银行支付通道支持任意银行(暂不支持信用卡)，且提现免手续费。</p>';
              $obj.html(html);
          }

      });
    };

    /**
     * 银行选择操作
     * @param $bankSelect 银行列表对象
     * @param $bankInput 银行号输入框对象
     * @param type 是否支持输入自动匹配选择 默认为true
     */
    var bankSelectBox=function($bankSelect){

        var $form=$bankSelect.closest("form");
        var bank=$bankSelect.data("bank");

        $bankSelect.on("click","li",function(){
            var img = $(this).children("img");
            var id=img.data("id");
            var bankName=img.attr("alt");
            /*$bankSelect.find('[name="bankName"]').val(bankName);*/
            $bankSelect.find('[name="payway"]').val(id);
            $bankSelect.find("li").removeClass("active");
            $(this).addClass("active");
        });

    };

    exports.sign = function() {
        common.leftNav({dl:3 ,dd:2});
        var $form = $("#bankSignForm");
        //支付通道取值
        payWay($("#payWrap"));

        bankSelectBox($("#payWrap"));
        $('[data-act="bankSelect"]').on("click",function(){
            bankSelector.box(function ($item) {
                var data = $item.data();
               /* var branchName=$item.data("name");
                var bankCode=$item.data('value');
                var bank=$item.data("bank");
                var bankId=$item.data("bankid");*/
                $form.find('[name="sign_bank_branch"]').val(data.name);//支行名称
                $form.find('[name="sign_bank_code"]').val(data.value);//联行号
                $form.find('[name="sign_bank_id"]').val(data.bankid);//银行大类id
               // $form.find('[name="bankRealName"]').val(bank);//银行大类名字

            });
        });
        //申请签约
        $form.validate({
            ignore: ".ignore",
            rules:{
                sign_bank_branch:{required: true},
                card_no:{required: true,isBankNo:true}

            },
            messages:{
                sign_bank_branch:{required: "请选择支行"},
                card_no:{required: "请输入银行卡号"}
            },
            submitHandler:function(f){
               common.ajaxHandle({
                   url:"/member/member/do_signBank",
                   data: $form.serialize(),
                   isLoading: true,
                   $btn:$form.find('[type="submit"]')
               });


            }

        });
    };
    /**
     * 申请解约
     */
    exports.unSign = function() {
        var account = require("account");
        common.leftNav({dl:3 ,dd:2});
        //申请解约
        $('[data-act="cancelBank"]').on("click",function(){
            //是否允许资金操作检查
            if(!account.checkAllowOperate()){
                return;
            }
            var virtualAccount = account.getVitrualAccount();
            if(virtualAccount>0) {
                common.error("您的附属账户还有资金，不可申请解约。请您选择先提现或者入金。");
                return;
            }
            layer.confirm("您确定要申请解约？",function(){
                account.checkPayPwd(function(){
                    common.ajaxHandle({
                        url: "/member/member/do_unSignBank"
                    })
                });
            });
        });
    }
});
